import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-12-13
 * Time: 21:59
 */
public class RedisGeneric {
    public static void test1(Jedis jedis) throws InterruptedException {
        System.out.println("setkey/getkey/ttl/expire");
        // 清空redis
        jedis.flushAll();
        // 设置key
        jedis.set("key1","111");
        jedis.set("key2","222");

        // 设置参数
        SetParams setParams = new SetParams();
        setParams.ex(100);
        setParams.xx();

        // 添加过期时间
        jedis.set("key2","222",setParams);

        // 获取key
        String value1 = jedis.get("key1");
        String value2 = jedis.get("key2");
        System.out.println("value1 = "+value1);
        System.out.println("value2 = "+value2);

        // 获取过期时间1
        long ttl_key2 = jedis.ttl("key2");
        System.out.println("ttl_key2 = "+ttl_key2);

        Thread.sleep(1000);

        // 获取过期时间2
        long ttl_key22 = jedis.ttl("key2");
        System.out.println("ttl_key22 = "+ttl_key22);

        // 添加过期时间2
        jedis.expire("key1",100);
        // 获取过期时间
        long ttl_key1 = jedis.ttl("key1");
        System.out.println("ttl_key1 = "+ttl_key1);

    }

    public static void test2(Jedis jedis) {
        System.out.println("type");

        jedis.flushAll();

        jedis.set("key1","111");
        String typeKey1 = jedis.type("key1");
        System.out.println("typeKey1 = "+typeKey1);

        jedis.rpush("key2","111","222","333");
        String typeKey2 = jedis.type("key2");
        System.out.println("typeKey2 = "+typeKey2);

        jedis.sadd("key3","111","222","333");
        String typeKey3 = jedis.type("key3");
        System.out.println("typeKey3 = "+typeKey3);

        jedis.hset("key4","zhangsan","111");
        String typeKey4 = jedis.type("key4");
        System.out.println("typeKey4 = "+typeKey4);

        jedis.zadd("key5",100,"zhangsan");
        String typeKey5 = jedis.type("key5");
        System.out.println("typeKey5 = "+typeKey5);

    }

    public static void test3(Jedis jedis) {
        System.out.println("keys");
        jedis.set("key1","111");
        jedis.set("key2","111");
        jedis.set("key3","111");
        jedis.set("key4","111");

        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
    }

    public static void main(String[] args) {
        // 创建redis连接池
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6379");
        // jdis表示一个与Redis服务器的连接（客户端）
        try (Jedis jedis = jedisPool.getResource()) {
            // 执行redis命令
//            String pong = jedis.ping();
//            System.out.println(pong);
//            test1(jedis);
//            test2(jedis);
            test3(jedis);
        }
    }
}
